<?php
$section = 'member';

$titre = _LB_BOOKING_MANAGEMENT;
$smarty->assign("titre", $titre);
if(!isMemberLoggedIn()){
	redirect(APPLICATION_URL_FO."/");
}
$backlink = Session::getBackLink($section);
$id = Request::getNumber("id");
$bdID = Request::getNumber("bdID");
$act = Request::getIntNumber('act');

$booking = new Booking();
if($id <=0 || !$booking->get($id)){
	redirect(APPLICATION_URL_FO."/");
}
if($booking->ID_MEMBER != $_SESSION['memberID']){
	redirect(APPLICATION_URL_FO."/");
}
if ($act == ACT_VALID_EDIT)
{
	$booking->STATUS = 0;
	$time = Request::get("TIME");
	$date = Request::get("DATE");
	$date = date_to_date_sql($date);
	$booking_date = $date. " {$time}";
	$service = $_POST['SERVICE'];
	if($booking->validate() === true){
		$delDetailSQL = "DELETE FROM BOOKING_DETAIL WHERE ID = {$bdID}";
		$delServiceSQL = "DELETE FROM BOOKING_DETAIL_SERVICE WHERE ID_BOOKING_DETAIL = {$bdID}";
		$booking_detail = new Booking_detail();
		$booking_detail->query($delServiceSQL);
		$booking_detail->query($delDetailSQL);
		$booking_detail = new Booking_detail();
		$booking_detail->ID_BOOKING = $id;
		$booking_detail->BOOKING_DATE = $booking_date;
		$booking_detail->ID_TECHNICIAN = 0;
		if($booking_detail->insert()){
			foreach($service as $serviceID){
				$booking_detail_service = new Booking_detail_service();
				$booking_detail_service->ID_BOOKING_DETAIL = $booking_detail->ID;
				$booking_detail_service->ID_SERVICE_BOOKING = $serviceID;
				$booking_detail_service->insert();
			}
		}
	}
	$booking->STATUS = 0;
	$booking->update();
	Mail_Notify::insertLog($id, 0);
	redirect(APPLICATION_URL_FO."/member/booking_detail/?id={$id}");
	redirect($backlink);
}

$bookingData = Booking_detail::getbookingDetail($id);
foreach($bookingData[$id]["TDATA"] as $ind => $data){
	if($ind != $bdID && isset($bookingData[$id]["TDATA"][$ind])){
		unset($bookingData[$id]["TDATA"][$ind]);
	}
}
$service = new Service_Booking();
$serviceData = $service->parseService2Array();
foreach($bookingData[$id]["TDATA"] as $data){
	$selectedDATE = date("Y-m-d",  strtotime($data["BOOKING_DATE"]));
	$selectedTIME = date("H:i:s",  strtotime($data["BOOKING_DATE"]));
}
$cfer = new Cfer(array(
	_LB_ACCUEIL => Utils::getHomeLink(),
	_LB_BOOKING_MANAGEMENT => Utils::getCurrentLink()));

$smarty->assign('cfer', $cfer);
$smarty->assign('bookingData', $bookingData[$id]);
$smarty->assign('bID', $id);
$smarty->assign('bdID', $bdID);
$smarty->assign('arrService', $serviceData);
$smarty->assign('selectedDATE', $selectedDATE);
$smarty->assign('selectedTIME', $selectedTIME);
$smarty->assign('backlink', $backlink);
$smarty->display('member_booking_adjust.html');

